﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;
using DatabaseGroup.SSIS.SourceAdapters;
using System;

namespace DatabaseGroup.UnitTests.SSIS.SourceAdapters
{
    [TestClass()]
    public class DelimiterStringConverterTest
    {
        private TestContext _testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return _testContextInstance;
            }
            set
            {
                _testContextInstance = value;
            }
        }

        [TestMethod()]
        [DeploymentItem("DatabaseGroup.SSIS.SourceAdapters.DelimitedFileSource.dll")]
        public void ToValueDelim_TwoDelimiters_ReturnExpectedValue()
        {
            Assert.AreEqual<string>("\r\n", DelimiterStringConverter_Accessor.ToValueDelim("{CR}{LF}"),
                                    "Expected CRLF value to be return based on passing the readable form in");
        }

        [TestMethod()]
        [DeploymentItem("DatabaseGroup.SSIS.SourceAdapters.DelimitedFileSource.dll")]
        public void ToReadableDelim_DelimitersAndNonDelimiters_ReturnExpectedValue()
        {
            Assert.AreEqual<string>("ABC{CR} K{LF}CFTab {t}ASD", DelimiterStringConverter_Accessor.ToReadableDelim("ABC\r K\nCF\tASD"), 
                                    "Expected non delimiter data to contain readable delimiters");
        }

        [TestMethod()]
        [ExpectedException(typeof(System.NotSupportedException), "Expect NotSupportedException to be thrown when passed a null value to be converted to int datatype")]
        public void ConvertTo_NullValue_ThrowNotSupportedException()
        {
            DelimiterStringConverter target = new DelimiterStringConverter();
            target.ConvertTo(null, CultureInfo.CurrentCulture, null, typeof(int));
        }

        [TestMethod()]
        public void ConvertTo_EmptyStringValue_ReturnExpectedValue()
        {
            DelimiterStringConverter target = new DelimiterStringConverter();
            string actual = (string) target.ConvertTo(null, CultureInfo.CurrentCulture, string.Empty, typeof (string));
            Assert.AreEqual<string>("<none>", actual, "Expected a readable <none> placeholder to be returned when converting an empty string value");
        }

        [TestMethod()]
        public void ConvertTo_MultipleDelimiterValues_ReturnExpectedValue()
        {
            DelimiterStringConverter target = new DelimiterStringConverter();
            string actual = (string) target.ConvertTo(null, CultureInfo.CurrentCulture, "\r\r\r\t\n\n \n\n", typeof (string));
            Assert.AreEqual<string>("{CR}{CR}{CR}Tab {t}{LF}{LF} {LF}{LF}", actual, "Expected delimiter values to be returned in their readable form");
        }

        [TestMethod()]
        public void ConvertFrom_EmptyReadableValue_ReturnExpectedValue()
        {
            DelimiterStringConverter target = new DelimiterStringConverter();
            string actual = (string) target.ConvertFrom(null, CultureInfo.CurrentCulture, "<none>");
            Assert.AreEqual<string>(string.Empty, actual, "Expected empty string to be returned when readable form equivalent is used");
        }

        [TestMethod()]
        public void ConvertFrom_MultipleReadableValues_ReturnExpectedValue()
        {
            DelimiterStringConverter target = new DelimiterStringConverter();
            string actual = (string) target.ConvertFrom(null, CultureInfo.CurrentCulture, "{CR}---{LF}Tab {t}BLAH{CR}{LF}");
            Assert.AreEqual<string>("\r---\n\tBLAH\r\n", actual, "Expected delimiter values to be returned based on their readable equivalents");
        }

        [TestMethod()]
        [ExpectedException(typeof(System.NotSupportedException), "Expect NotSupportedException to be thrown when passed a null value to be converted to int datatype")]
        public void ConvertFrom_NullValue_ThrowNotSupportedException()
        {
            DelimiterStringConverter target = new DelimiterStringConverter();
            target.ConvertFrom(null, CultureInfo.CurrentCulture, 1);
        }

        [TestMethod()]
        public void DelimiterStringConverter_DefaultConstructor_ReturnNewInstance()
        {
            DelimiterStringConverter target = new DelimiterStringConverter();
        }
    }
}
